package com.shinhye.elephantsafe.recevier;

import java.util.List;

import com.shinhye.elephantsafe.R;
import com.shinhye.elephantsafe.utils.SystemInfoUtils;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.text.format.Formatter;
import android.util.Log;
import android.widget.RemoteViews;

public class KillProcessReceiver extends BroadcastReceiver {
	private String TAG = "KillProcessReceiver";

	@Override
	public void onReceive(Context context, Intent intent) {
		Log.i(TAG, "接收到Widget发的广播，一键清理进程！");
		
		ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningAppProcessInfo> infos = am.getRunningAppProcesses();
		for (RunningAppProcessInfo info : infos) {
			am.killBackgroundProcesses(info.processName);
		}
		
		AppWidgetManager awm = AppWidgetManager.getInstance(context);
		RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_elephantsafe);
		views.setTextViewText(R.id.tv_process_count, "正在运行的软件：" + SystemInfoUtils.getRunningProcessCount(context));
		views.setTextViewText(R.id.tv_process_memory, "可用内存：" + Formatter.formatFileSize(context, SystemInfoUtils.getAvailRAM(context)));
		
		ComponentName componentName = new ComponentName(context, ESafeWidget.class);
		awm.updateAppWidget(componentName, views);
	}

}
